import dynamic from "@/api/dynamic.js";
import release from "@/api/release.js";
import { currentTime, sevenDaysLater } from "@/utils/formTime.js";
import Util from "@/api/util.js";
import User from '@/api/user.js'
import { isObject, isArray} from '@/utils/is.js'

// 子组件调取的方法
export default {
  provide(){
    return{
      attention: this.attention // 关注方法
    }
  },
  methods:{
    cancelApplyForFn () { // 关闭申请帮卖
      this.isHelpSellingShow = false
    },
    getOthersInfoFn (accountId) {
      let params = {accountId: accountId}
      User.getOthersInfo(params).then(res => {
        if (res.code == 200) {
          this.$store.commit('OTHERS_INFO_DATA', res.data)
          if (res.data.isMyself==0&&this.routeData.applyForHelp) this.getRelationFn()
        }
      })
    },
    getRelationFn () {
      User.getRelation({idCode: this.othersInfoData.idCode || this.routeData.idCode}).then(res => {
        if (res.code == 200) {
          this.getRelationType = res.data.relation
          if ([0,1,3].includes(this.getRelationType)) {
            this.isHelpSellingShow = true
          } else {
            this.isHelpSellingShow = false
          }
        }
      })
    },
    // 返回
    goBackFn () {
      let routeArr = getCurrentPages()
      console.log(routeArr,111);
      if (!routeArr||routeArr.length==1||routeArr[routeArr.length-2].route&&routeArr[routeArr.length-2].route=='pagesA/release/issueEdit') {
        wx.switchTab({url: '/pages/index/index'})
        return
      }
      this.navigateBack()
    },
    goHomePage(){
      const item = this.videoList[this.swiperIndex]
      let routes = getCurrentPages()
      let pageUrlArr = ['pagesB/personalCenter/homePage']
      if (routes.length>=2&&pageUrlArr.includes(routes[routes.length-2].route)) {
        this.navigateBack({
          delta: 1
        })
        return
      }
      this.navigateTo({url: `/pagesB/personalCenter/homePage?accountId=${item.accountId}&index=${this.swiperIndex}`});
    },
    // 关注作者
    attention(type){
      const item = this.videoList[this.swiperIndex]
      dynamic.follow({followAccountId: item.accountId,isFollow: type}).then((res)=>{
        this.videoList[this.swiperIndex].followStatus = type
        this.$store.commit('FOLLOW_TYPE', this.videoList[this.swiperIndex])
        uni.showToast({
          icon: 'none',
          mask: true,
          title: '关注成功！'
        })
      })
    },
    // 点赞
    clickLike(){
      const item = this.videoList[this.swiperIndex]
      let params = {
         type: 1,
         sourceId: item.dynamicId,
         isLike: item.isLike ? 0 : 1 // 0取消点赞1点赞
      }
      dynamic.giveLike(params).then(res=>{
        this.videoList[this.swiperIndex].isLike = params.isLike // 改变状态
        params.isLike ? this.videoList[this.swiperIndex].likeCount++ : this.videoList[this.swiperIndex].likeCount-- // 改变点赞数
      })
    },
    // 帮卖
    clickBestead(type){
      // const this_ = this
      // const item = this.videoList[this.swiperIndex]
      // if(item.isMyself === 1 && item.isGroup === 2 && !this_.goodsEndTiem){// 自己团已结束
      //   uni.showModal({
      //     title: `提示`,
      //     content: `该团已结束，是否重新开启团购？`,
      //     confirmText: "确认",
      //     confirmColor: "#e44435",
      //     success(res) {
      //       if (res.confirm) {
      //         this_._updateEndTime(2,false,type); // 重启并且重新调用
      //       }
      //     }
      //   })
      //   return
      // }

      if(type === 1){
        this.configBestead() // 一键帮卖
        // this.oneSales = true
      }else{// 邀请帮卖
        this.moreShow = false
        this.groupsShare(2) // 邀请帮卖
      }
    },
    closeSoldForFn () {
      this.isSoldForHelpSellShow = false
    },
    // 已经帮卖过刷新自己的视频
    refreshOwnFn (data) {
      console.log(data,'datadatadata')
      this.getDynamicDetails(data,9) // 刷新第一条数据
    },
    // 确定一键帮卖
    configBestead(){
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      if (this.configBesteadDisable) return
      this.configBesteadDisable = true // 防止重复点击
      release.oneKeyHelpSell({ dynamicIdLv1: this.videoList[this.swiperIndex].dynamicId }).then(res=>{
        wx.hideLoading()
        if (res.code == 200) {
          // this.videoList[this.swiperIndex].helpSellStatus = 2 // 状态改为正在帮卖
          this.configBesteadDisable = false
          this.getDynamicDetails(res.data,1)
          console.log('一键帮卖成功');
        }
        if (res.code == 4014) {
          this.configBesteadDisable = false
          this.soldForHelpSellId = res.data
          this.isSoldForHelpSellShow = true
        }
      })
    },
    // 编辑后帮卖
    editBestead(){
      this.navigateTo({
        url: `/pagesA/release/issueEdit?dynamicId=${this.videoList[this.swiperIndex].dynamicId}&operateType=1&isHelpSell=1`
      });
    },

    // 分享 type:1 头像下分享点击； 2更多操作，微信点击； 3更多操作，朋友圈点击
    openShare(type){
      const this_ = this
      const item = this.videoList[this.swiperIndex]
      // if(item.isMyself === 1 && item.isGroup === 2 && !this_.goodsEndTiem){// 自己团已结束
      //   uni.showModal({
      //     title: `提示`,
      //     content: `该团已结束，是否重新开启团购？`,
      //     confirmText: "确认",
      //     confirmColor: "#e44435",
      //     success(res) {
      //       if (res.confirm) {
      //         this_._updateEndTime(2,type); // 重启并且重新调用
      //       }
      //     }
      //   })
      //   return
      // }

      this_.moreShow = false
      if ([1,2].includes(type)) {
        this_.groupsShare()
      }
      if (type == 3) {
        this_.shareVideoData = item
        this_.posterShareShow = true
        // this_.createPosterFn()
      }
    },
    // 获取单个创作详情
    async getDynamicDetails(dynamicId,type) {
      dynamic.getDynamicDetails({ dynamicId: dynamicId }).then((res) => {
        if (res.code === 200) {
          this.videoList[this.swiperIndex] = res.data
          this.oneSales = false
          this.groupsShare(type)
        }
      })
    },
    releaseSuccessFn () {
      // 处理发布开团过来 start
      this.sharePublishData = {
        url: this.$store.state.openGroupShareData.url,
        type: this.routeData.isReleasePopup,
        title: this.videoList[0].title ? this.videoList[0].title : `发布了一个新的${this.videoList[0] && this.videoList[0].mediaType === 1 ? '内容' : '视频'}，快来看看吧！` // 动态发布成功
      }
      if (this.routeData.isReleasePopup == 2) {// 开团成功
        this.sharePublishData.title = this.videoList[0].title?this.videoList[0].title : `发布了一个新的${this.videoList[0] && this.videoList[0].mediaType === 1 ? '' : '视频'}团购，快来购买吧！`
      }
      this.isPublishPopup = true
      // 处理发布开团过来 end
    },
    // 关闭发布成功弹窗
    closeSharePublishFn (e) {
      this.isPublishPopup = e
    },
    // 生成海报
    posterFn (data) {
      this.shareVideoData = data ? data : this.videoList[this.swiperIndex]
      this.isSharePublicPage = false
      this.posterShareShow = true
    },
    // 关闭分享海报弹窗
    cancelSharePosterFn () {
      this.posterShareShow = false
    },
    // 关闭分享弹窗
    cancelShareFn (e) {
      this.isSharePublicPage = e
    },
    // 视频流分享
    groupsShare (type) {
      this.shareVideoData = this.videoList[this.swiperIndex]
      if (type) { // 有传类型带上类型
        this.shareVideoData = {
          ...this.shareVideoData,
          isShareType: type
        }
      }
      if (type!=9) this.isSharePublicPage = true
    },
    // 显示隐藏动态 type:2隐藏 type:1显示
    switchHide(type){
      let this_ = this
      let params = {
        dynamicId: this_.videoList[this_.swiperIndex].dynamicId,
        openStatus: type
      }
      dynamic.setOpenStatus(params).then(res=>{
        this_.videoList[this_.swiperIndex].openStatus = type // 隐藏显示状态更改
        this.setIsRefreshType(true)
        console.log(`${type === 2 ? '隐藏' : '显示'}成功`);
      })
    },
    // 删除该动态
    removeDynamic(){
      let this_ = this
      wx.showModal({
        title: "删除后不可恢复，是否确认删除？",
        confirmText: "确认",
        confirmColor: "#e44435",
        success(res) {
          if (res.confirm) {
            const item = this_.videoList[this_.swiperIndex]
            let params = {
              sourceId: item.dynamicId,
              type: 1 // 1创作2评论3回复
            }
            dynamic.deleteComment(params).then((res) => {
              this_.moreShow = false
              
              this_.routeData.finish = false
              this_.routeData.currentPage = 1
              this_.videoList = []
              this_.swiperIndex = 0
              this_.getData('delete')
            })
          }
        }
      })
    },
    // 开发或者结束动态 type:2重启 type:1结束
    // dynamicStatus(type){
    //   let this_ = this
    //   uni.showModal({
    //     title: `确认${type === 2 ? '重启' : '结束'}该团购活动？`,
    //     confirmText: "确认",
    //     confirmColor: "#e44435",
    //     success(res) {
    //       if (res.confirm) {
    //         this_._updateEndTime(type)
    //       }
    //     }
    //   })
    // },
    // _updateEndTime(type,isShare,isBestead){
    //   let this_ = this
    //   const item = this_.videoList[this_.swiperIndex]
    //   let params = {
    //     dynamicId: item.dynamicId,
    //     isEnd: type
    //   }
    //   dynamic.updateEnd(params).then(res=>{
    //     if(type === 2){ // 重启处理
    //       this_.videoList[this_.swiperIndex].endTime = sevenDaysLater(7) // 将当前团结束时间置为七天后
    //       this_._beginTimer()
    //     }else{ // 结束处理
    //       this_.videoList[this_.swiperIndex].endTime = currentTime() // 将当前团结束时间置为现在
    //       this_.goodsEndTiem = ''
    //     }
 
    //     // 刷新商品数据 start
    //     const refsName = 'bottomMsg'+this_.videoList[this_.swiperIndex].dynamicId
    //     if(isArray(this_.$refs[refsName])){
    //       this_.$refs[refsName][0].onCreated() 
    //     }else{ 
    //       this_.$refs[refsName].onCreated() // 刷新商品数据
    //     }

    //     console.log(`${type === 2 ? '重启' : '结束'}成功`);
    //     // 刷新商品数据 end
        
    //     if(type === 2 && isShare){ // 重启完，直接打开分享
    //       setTimeout(() => {
    //         this.openShare(isShare)
    //       }, 1000);
    //     }else if(type === 2 && isBestead){
    //       setTimeout(() => {
    //         this.clickBestead(isBestead)
    //       }, 1000);
    //     }
    //   })
    // },
    // 是否置顶 type:1置顶 type:0取消置顶
    isTop(type){
      const item = this.videoList[this.swiperIndex]
      let params = {
        dynamicId: item.dynamicId,
        isTop: type
      }
      dynamic.topDynami(params).then(res => {
        this.videoList[this.swiperIndex].isUserTop = type // 更改置顶状态
        // 刷新页面数据
        this.setIsRefreshType(true)
        console.log(`${type === 1 ? '置顶' : '取消置顶'}成功`);
      })
    },
    // 打开底部弹窗
    openBottom(goodsList, current){
      this.goodsList = goodsList
      this.goodsCurrent = current
      this.bottomShow = true
    },
    playvolumeFn (mun) {
      return Util.playvolume(mun)
    }
  }
}